home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / mail / YAMscripts.lha / Fileserver2.rexx < prev    next >
OS/2 REXX Batch file  |  1997-06-27  |  12KB  |  437 lines

  1. /* Fileserver.rexx by Kai Nikulainen - 11-Apr-97 v2.2
  2. ** Check http://www.utu.fi/~knikulai/ARexx.html for more useful scripts! 
  3. ** 
  4. **               CD - change current directory. CD without arguments returns to root
  5. **         SEND,GET - send the requested file
  6. ** ALLFILES,LISTALL - send a listing of all files in the directory
  7. **   DIR,INDEX,LIST - send a listing of a directory.  Path is optional.
  8. **   INFO,SHOW,VIEW - display file size, filenote and archive contents
  9. **             QUIT - further lines in message body are ignored
  10. **             HELP - send the helpfile
  11. **
  12. ** If you start the script from shell by typing 'rx fileserver2.rexx index' and the
  13. ** variable allfiles contains a valid filename it will create an index file which 
  14. ** can be used to speed up the ALLFILES command.  Please note that if allfiles
  15. ** variable is defined, the index sent may be out of date.
  16. **
  17. ** If you have any problems or suggestions, mail me at knikulai@utu.fi
  18. **
  19. ** This script would not have been written without a request from
  20. ** Jon Jeffels <tiger@zetnet.co.uk> 
  21. ** 
  22. */
  23. options results
  24. parse arg opt1
  25. call addlib('rexxsupport.library',0,-30,0)
  26.  
  27. /* 
  28. ** Change these variables to fit your system
  29. */
  30. folder=0                /* 0=Incoming, 1=Outgoing, etc..             */
  31.                     /* Change this to 1 for testing             */
  32. maxline=75                /* Line length for directory listings        */
  33. dirle=8                    /* How many letters should be reserved for   */
  34.                     /* dir names in all files list?             */
  35. readme_ext='.README'            /* Files with name ending with this string   */
  36.                     /* are not displayed in filelistings, but    */
  37.                     /* they are shown with VIEW,INFO and SHOW    */
  38.     subj='REQUEST'            /* This needs to be the subject, otherwise   */
  39.                     /* message isn't processed (case is ignored) */
  40.     sdir='packed:Fileserver/'        /* Root directory, must end with / or :      */
  41.    logon='Packed:Fileserver.logon'    /* This message is copied to the beginning of*/
  42.                       /* a reply. For example inform about changes */
  43. allfiles=''                /* This is used for ALLFILES command, if the */    
  44.                     /* string is not empty and the file exists   */
  45. helpfile='Packed:Fileserver.help'       /* This file is reply to HELP command         */
  46.   seslog='packed:session.log'        /* Stores all requests and their results     */
  47. seslogto=''                /* If this is not empty, session log will be */
  48.                     /* mailed there when the script ends          */
  49.  logfile='packed:fileserver.log'    /* Session logs are stored here */
  50.      log='yes'                /* If something else than yes, no logging    */
  51. autosend='yes'                /* If yes, messages are sent by the script   */
  52.   delmsg='yes'                /* Deletes messages after processing. If this*/
  53.                       /* is something else than yes, messages are  */
  54.                     /* just marked as deleted                    */
  55.  
  56. replsubj='Fileserver reply from knikulai@utu.fi'
  57.  
  58. type='c:type'        /* AmigaDOS command type */
  59. list='c:list'        /* AmigaDOS command list */
  60. copy='c:copy'
  61. rename='c:rename'    /* Yet another command */
  62. lha='c:lha'        /* This is used to display .lha and .lzh files */
  63. unzip='c:unzip'        /* .zip */
  64. unlzx='c:unlzx'        /* .lzx */
  65.  
  66.  
  67.  
  68.  
  69. /*
  70. ** BE VERY CAREFUL WHEN YOU CHANGE SOMETHING BELOW!!!
  71. */
  72.  
  73. if upper(opt1)='INDEX' & allfiles~='' then do
  74.     call CreateListing(sdir,all)
  75.     address command copy 't:list.output' allfiles
  76.     call delete('t:list.output')
  77.     exit
  78.     end
  79.  
  80. if pos(right(sdir,1),':/')=0 then sdir=sdir'/'
  81.  
  82. /* Open seslog, if needed */
  83. log_it=0
  84. listed_already=0
  85. if upper(log)='YES' then log_it=open(4,seslog,'w')
  86.  
  87. address 'YAM'
  88. 'Hide'
  89. 'SetFolder' folder
  90.  
  91. 'GetMailInfo Active'    /* Remember current message */
  92. active=result
  93. if rc>0 then active=0
  94.  
  95. 'GetFolderInfo Max'    /* How many messages are there? */
  96. n=result
  97.  
  98. /* Check all messages in folder */
  99. do m=0 to n-1
  100.     'SetMail' m
  101.     /* proces if correct header */
  102.     'GetMailInfo Subject'
  103.     if upper(result)=upper(subj) then do
  104.         'GetMailInfo From'
  105.         s=result
  106.         'GetMailInfo File'
  107.         msgfile=result
  108.         call Process(s,result)
  109.         if upper(delmsg)='YES' then
  110.             call delete(msgfile)
  111.         else
  112.             'MailDelete'
  113.         end
  114.     end
  115.  
  116. if upper(delmsg)='YES' then 'MailUpdate'
  117.  
  118. if log_it then do
  119.     call close(4)                /* Close session log  */
  120.     address command type '>>'logfile seslog    /* Copy the contents */
  121.     end
  122.  
  123. if seslogto~='' & log_it then do
  124.     'MailWrite'
  125.     'WriteMailTo "'seslogto'"'
  126.     'WriteSubject "Session log from fileserver"'
  127.     'WriteLetter' seslog
  128.     'WriteQueue'
  129.     end
  130.  
  131. address command 'delete >nil:' seslog
  132. if upper(autosend)='YES' then 'MailSendAll'
  133.  
  134. if listed_already then call delete('t:full.list')
  135. 'SetMail' active
  136. 'Show'
  137. exit
  138.  
  139. CopyFile:
  140. parse arg copyname
  141.     if exists(copyname) then do
  142.         call open(cp,copyname,'r')
  143.         do while ~eof(cp)
  144.             r=readln(cp)
  145.             call writeln(2,r)
  146.             end
  147.         call close(cp)
  148.         if log_it then call writeln(4,copyname 'copied to reply')
  149.         end
  150.     else
  151.         if log_it then call writeln(4,'*** Failed to copy' copyname 'into reply ***')
  152. return
  153.  
  154. Process:
  155. parse arg sender,file
  156.     if log_it then 
  157.         call writeln(4,left('0a'x sender '--' date() '--' time()' ',71,'-'))
  158.     call open(1,file,'r')    
  159.     call open(2,'t:fileserver.reply','w')
  160.     call CopyFile(logon)
  161.     att=0
  162.     dir=sdir
  163.     help=0
  164.     shrthlp=0
  165.     'MailWrite'
  166.     'WriteMailTo "'sender'"'
  167.     'WriteSubject "'replsubj'"'
  168.  
  169.     /* Skip message headers */
  170.     do until r='' | eof(1)
  171.         r=strip(readln(1))
  172.         end
  173.  
  174.     /* process body: until empty or quit*/
  175.     do until upper(r)='QUIT' | eof(1)
  176.         r=translate(strip(readln(1)),'/','\')
  177.         call writeln(2,'>' r)
  178.         cmd=upper(word(r,1))
  179.         ok=0
  180.         if cmd='ALLFILES' | cmd='LISTALL' then do
  181.             call send_allfiles
  182.             ok=1
  183.             end
  184.         if cmd='QUIT' then ok=1
  185.         if cmd='GET' | cmd='SEND' then do
  186.             call send_file(word(r,2))
  187.             ok=1
  188.             end
  189.         if cmd='CD' then do
  190.             call change_dir(word(r,2))
  191.             ok=1
  192.             end
  193.         if cmd='VIEW' | cmd='SHOW' | cmd='INFO' then do
  194.             call view_file(word(r,2))
  195.             ok=1
  196.             end
  197.         if cmd='INDEX' | cmd='LIST' | cmd='DIR' then do
  198.             call send_index(word(r,2))
  199.             ok=1
  200.             end
  201.         if cmd='HELP' then do
  202.             call send_help
  203.             ok=1
  204.             end
  205.         if ~ok & r~='' then do
  206.             call writeln(2,'Unknown command!')
  207.             if log_it then
  208.                             call writeln(4,'*** Unknown command' r '***')   
  209.             if ~shrthlp then do
  210.                 call writeln(2,'Try: ALLFILES, CD [path], DIR [path], GET <file>, HELP, INDEX [path],')
  211.                 call writeln(2,'     INFO <file>, LIST [path], LISTALL, SEND <file>, SHOW <file>')
  212.                 call writeln(2,'     or VIEW <file>')
  213.                 call writeln(2,'[path] is optional and <file> means a file in the current directory')
  214.                 shrthlp=1
  215.                 end
  216.             end
  217.         address 'YAM'
  218.         end
  219.     call close(1)
  220.     call close(2)
  221.     'WriteLetter t:fileserver.reply'
  222.     'WriteQueue'
  223. return
  224.  
  225. send_help:
  226.     if help then do
  227.         call writeln(2,'Once should be enough...')
  228.         return
  229.         end
  230.     call CopyFile(helpfile)
  231.     help=1
  232. return
  233.  
  234. send_allfiles:
  235.     if allfiles~='' & exists(allfiles) then do
  236.         call CopyFile(allfiles)
  237.         return
  238.         end
  239.     if ~listed_already then do
  240.         call CreateListing(sdir,all)
  241.         address command rename 't:list.output t:full.list'
  242.         listed_already=1
  243.         end
  244.     call CopyFile('t:full.list')
  245.     if log_it then call writeln(4,'List of all files sent')
  246. return
  247.  
  248. send_index:
  249. parse arg path
  250.     if left(path,1)~='/' then
  251.         foo=dir || path
  252.     else
  253.         foo=sdir || substr(path,2)
  254.     if exists(foo) then do
  255.         call CreateListing(foo,'')
  256.         call CopyFile('t:list.output')
  257.         if log_it then call writeln(4,'Index of' foo 'sent')
  258.         call delete('t:list.output')    
  259.         end
  260.     else do
  261.         call writeln(2,'Directory does not exist')
  262.         if log_it then
  263.                         call writeln(4,'*** Tried list' foo '***')   
  264.         end    
  265. return
  266.  
  267. change_dir:
  268. parse arg newdir
  269.     if newdir='' then do
  270.         dir=sdir
  271.         call writeln(2,'Directory changed back to root.')
  272.         if log_it then
  273.             call writeln(4,'CD to root')
  274.         return
  275.         end
  276.     if pos(right(newdir,1),'/:')=0 then newdir=newdir'/'
  277.     if left(newdir,1)='/' then
  278.         foo=sdir || substr(newdir,2)
  279.     else
  280.         foo=dir || newdir
  281.     if exists(foo) then do
  282.         inf=statef(foo)
  283.         if word(inf,1)='DIR' then do
  284.             dir=foo
  285.             call writeln(2,'Ok')
  286.             if log_it then
  287.                 call writeln(4,'CD to' newdir 'ok')
  288.             end
  289.         else do
  290.             call writeln(2,'It is not a directory!')
  291.             if log_it then
  292.                 call writeln(4,'*** Tried to cd to a file' newdir '***')
  293.             end
  294.         end
  295.     else do
  296.         call writeln(2,'No such subdirectory!')
  297.         if log_it then
  298.             call writeln(4,'*** Tried to cd to a nonexistent' newdir '***')
  299.         end
  300. return
  301.  
  302.  
  303. view_file:
  304. parse arg filename
  305.     foo=dir || filename
  306.     if exists(foo) then do
  307.         inf=statef(foo)
  308.         parse var inf ft byt blo flgs days min ticks comment
  309.         call writeln(2,'Filetype:'ft ' Size:'byt 'bytes  Last modified:'date('n',days,'i'))
  310.         call writeln(2,strip(comment))
  311.         if exists(foo || readme_ext) then call CopyFile(foo || readme_ext)
  312.         if log_it then
  313.             call writeln(4,'Displayed file info of' foo)
  314.         ext=upper(right(filename,4))
  315.         ar=0
  316.         if ext='.LHA' | ext='.LZH' then do
  317.             address command lha '>t:arc.list v' foo
  318.             ar=1
  319.             end
  320.         if ext='.ZIP' then do
  321.             address command unzip '>t:arc.list -v' foo
  322.             ar=1
  323.             end
  324.         if ext='.LZX' then do
  325.             address command unlzx '>t:arc.list v' foo
  326.             ar=1
  327.             end
  328.         if ar=1 then do
  329.             call CopyFile('t:arc.list')
  330.             call delete('t:arc.list')
  331.             if log_it then call writeln(4,'Displayed archive contents of' foo)
  332.             end
  333.         end
  334.     else do
  335.         call writeln(2,'No such file!')
  336.         if log_it then
  337.             call writeln(4,'*** Failed to display info of' foo '***')
  338.         end
  339. return
  340.  
  341. send_file:
  342. parse arg fn
  343.     if exists(dir || fn) then do
  344.         ft='application/octet-stream'
  345.         p=lastpos('.',fn)
  346.         if p>0 then do
  347.             ext=upper(substr(fn,p+1))
  348.             if ext='GIF' then ft='image/gif'
  349.             if ext='JPG' then ft='image/jpeg'
  350.             if ext='WAV' then ft='audio/basic'
  351.             if ext='TXT' then ft='text/plain'
  352.             if ext='REXX' then ft='text/plain'
  353.             if ext='MPG' then ft='video/mpeg'
  354.             end
  355.         'WriteAttach' dir || fn '"" MIME' ft
  356.         att=att+1
  357.         call writeln(2,att'. file included as an attachment.')
  358.         if log_it then
  359.             call writeln(4,'File' dir||fn 'sent')
  360.         end /* if exists(dir ll fn) then */
  361.     else do
  362.         call writeln(2,'File not found!')
  363.         if log_it then
  364.             call writeln(4,'*** Could not send non-existent' dir || fn '***')
  365.         end
  366. return
  367.  
  368. CreateListing:
  369. parse arg lstdir,opt
  370.  
  371. if pos(right(lstdir,1),'/:')=0 then lstdir=lstdir'/'
  372. if opt='' then
  373.     address command list '>t:lst.tmp1 lformat="%P%S %B %C"' lstdir
  374. else
  375.     address command list '>t:lst.tmp1 lformat="%P%S %B %C" all files' lstdir
  376.  
  377. call open(in,'t:lst.tmp1','r')
  378. call open(out,'t:lst.tmp2','w')
  379. longest=0
  380.  
  381. do while ~eof(in)
  382.     r=readln(in)
  383.     if length(r)>length(rootdit) then r=substr(r,length(lstdir)+1)
  384.     parse var r name .
  385.     po=lastpos('/',name)
  386.     if longest<length(name)-po then longest=length(name)-po
  387.     if upper(right(name,7))~=upper(readme_ext) then call writeln(out,r)
  388.     end
  389.  
  390. call close(in)
  391. call delete('t:lst.tmp1')
  392. call close(out)
  393.  
  394. call open(in,'t:lst.tmp2','r')
  395. call open(out,'t:list.output','w')
  396.  
  397. do while ~eof(in)
  398.     r=readln(in)
  399.     if r~='' then do
  400.         parse var r name size comment
  401.         comment=strip(comment)
  402.         if size='empty' then size=0
  403.         if comment='' then comment='(no description available)'
  404.         if datatype(size)='NUM' then do
  405.             if size<2000 then
  406.                 size=trunc(size/2+0.5)'K'
  407.             else
  408.                 size=trunc(size/2048+0.05)'M'
  409.             end
  410.         sc=length(name)-length(compress(name,'/'))
  411.         le=longest+sc*dirle/* Dir names should be at most dirle letters */
  412.         line=left(name,le) right(size,4) comment
  413.         if length(line)>maxline then do    
  414.             po=lastpos(' ',line,maxline)
  415.             rest=left('',le+6) || substr(line,po+1)
  416.             line=left(line,po-1)
  417.             do while strip(rest)~=''
  418.                 po=lastpos(' ',rest,maxline)
  419.                 if po<=le+6 then po=maxline
  420.                 if length(rest)>maxline then do
  421.                     line=line || '0a'x || left(rest,po-1)
  422.                     rest=left('',le+6) || substr(rest,po+1)
  423.                     end
  424.                 else do
  425.                     line=line || '0a'x || rest
  426.                     rest=''
  427.                     end
  428.                 end
  429.             end
  430.         call writeln(out,line)
  431.         end
  432.     end
  433. call close(in)
  434. call delete('t:lst.tmp2')
  435. call close(out)
  436. return
  437.